using System.IO;
using Framework.Core.Recorder;
using Xunit;

namespace Framework.Tests.Core.Recorder.AcceptanceTests
{
    public class OverrideAndIgnoreTest
    {
        public class NotSerializable
        {
            public string Name { get; private set; }

            public NotSerializable(string name)
            {
                Name = name;
            }
        }

        public interface IToRecord
        {
            bool IsPouet(NotSerializable notSerializable);
            bool IsPouet(string toCompare);
        }

        class ToRecord : IToRecord
        {
            public bool IsPouet(NotSerializable notSerializable)
            {
                return notSerializable.Name == "pouet";
            }

            public bool IsPouet(string toCompare)
            {
                return toCompare == "pouet";
            }
        }

        [Fact]
        public void Should_test_override_func_feature()
        {
            var recorder = new HardMock().BuildRecorderOver<IToRecord>(new ToRecord());

            Assert.False(recorder.IsPouet("toto"));
            Assert.True(recorder.IsPouet("pouet"));

            using (var stream = new MemoryStream())
            {
                recorder.Snapshot(stream);
                var replayer = new HardMock().BuildMockFrom<IToRecord>(stream);

                replayer.Override(x=>x.IsPouet("toto"), true);

                Assert.True(replayer.IsPouet("toto"));
                Assert.True(replayer.IsPouet("pouet"));
            }
        }

        [Fact]
        public void Should_test_override_by_ignoring_arguments_feature()
        {
            var recorder = new HardMock(typeof(NotSerializable)).BuildRecorderOver<IToRecord>(new ToRecord());

            Assert.False(recorder.IsPouet(new NotSerializable("tutu")));

            using (var stream = new MemoryStream())
            {
                recorder.Snapshot(stream);
                var replayer = new HardMock(typeof(NotSerializable)).BuildMockFrom<IToRecord>(stream);

                Assert.False(replayer.IsPouet(new NotSerializable("pouet")));
                Assert.Throws<NotRecordedException>(() => replayer.IsPouet("poeut"));
            }
        }
    }
}